<!--  -->
<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>新闻管理</el-breadcrumb-item>
      <el-breadcrumb-item>新闻列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区 -->
    <el-card>
      <el-row :gutter="20">
        <el-col :span="10"
          ><div class="text item">
            <el-input placeholder="请输入内容" class="input-with-select"
            v-model="queryInfo.query" clearable @clear="getNewsList">
              <el-button slot="append" icon="el-icon-search"></el-button>
            </el-input></div
        ></el-col>
      </el-row>
      <!-- 订单列表区 -->
      <el-table :data="newsInfo" highlight-current-row  border>
        <el-table-column
          type="index"
          width="40">
        </el-table-column>
        <el-table-column
          property="newsImages"
          label="新闻图片"
          width="120">
          <template slot-scope="scope">
      　　　　<img :src="scope.row.newsImages" class="headimag"/>
      　　</template>
        </el-table-column>
        <el-table-column
          property="type"
          label="新闻类型"
          width="100"><template slot-scope="scope">
            <span v-show="scope.row.type === '1'">小宠饲养</span>
            <span v-show="scope.row.type === '2'">小宠训练</span>
            <span v-show="scope.row.type === '3'">小宠医疗</span>
          </template>
        </el-table-column>
        <el-table-column
          property="newsTitle"
          label="新闻标题"
          width="150">
          <template slot-scope="scope">
            <div class="newsTitle">{{scope.row.newsTitle}}</div>
          </template>
        </el-table-column>
        <el-table-column
          label="新闻内容" property="newsText">
          <template slot-scope="scope">
            <div class="newsContent">{{scope.row.newsText}}</div>
          </template>
        </el-table-column>
        <el-table-column
          property="newsStatus"
          label="发布状态"
          width="100">
          <template slot-scope="scope">
            <el-tag type="success" v-if="scope.row.newsStatus===1">已发布</el-tag>
            <el-tag type="danger" v-else>未发布</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          property="likeCount"
          label="点赞量"
          width="80">
        </el-table-column>
        <el-table-column
          label="操作" width="180">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" @click="showEditDialog(scope.row)" size="mini">修改</el-button>
            <el-button type="danger" icon="el-icon-delete" @click="removeNewsInfo(scope.row.id)" size="mini">删除</el-button>
          </template>
        </el-table-column>
        
      </el-table>
        <!-- 分页 -->
      <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryInfo.pageNum"
      :page-sizes="[10,15,20]"
      :page-size="queryInfo.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
      </el-pagination>
    </el-card>
    <!-- 修改新闻对话框 -->
    <el-dialog title="修改新闻信息" width="50%" :visible.sync ="dialogEditVisible"
    @close="editDialogClose">
      <el-form
        :model="updateNewsInfo"
        ref="editFormRef"
        label-width="100px"
        :rules="newsRules"
        class="demo-ruleForm "
      >
        <el-form-item label="新闻图片" prop="newsImages">
          <img :src="updateNewsInfo.newsImages" alt="" class="headimag" >
          <OssUpload v-model="updateNewsInfo.newsImages"></OssUpload>
        </el-form-item>
        <el-form-item label="新闻类型" prop="type">
          <el-select v-model="updateNewsInfo.type" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="新闻标题" prop="newsTitle">
          <el-input v-model="updateNewsInfo.newsTitle"></el-input>
        </el-form-item>
        <el-form-item label="新闻内容" prop="newsText">
          <textarea v-model="updateNewsInfo.newsText" class="updateNewsContent"></textarea>
        </el-form-item>
        <el-form-item label="发布状态" prop="newsStatus">
          <el-radio-group v-model="updateNewsInfo.newsStatus" >
            <el-radio label='0' border>未发布</el-radio>
            <el-radio label='1' border>已发布</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogEditVisible = false">取 消</el-button>
        <el-button type="primary" @click="updateNews">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import OssUpload from '../../components/OssUpload.vue'
export default {
  name:'News',
  data () {
    return {
      queryInfo:{
        query:'',
        // 当前页码
        pageNum:1,
        // 当前每页显示多少条数据
        pageSize:10
      },
      dialogEditVisible:false,
      total:10,
      newsInfo:[],
      // 修改信息
      updateNewsInfo:{},
       options: [{
          value: '1',
          label: '小宠饲养'
        }, {
          value: '2',
          label: '小宠训练'
        }, {
          value: '3',
          label: '小宠医疗'
        }],
        newsRules:
          {
             newsTitle:{ required: true, message: '请输入新闻标题名称', trigger: 'blur' },
              newsContent:{ required: true, message: '请输入新闻内容', trigger: 'blur' },
          }
    };
  },
  components:{
   OssUpload 
  },
  created(){
    this.getNewsList()
  },
  methods:{
    // 监听pageSize改变
    handleSizeChange(newSize){
      // console.log(newSize)
      this.queryInfo.pageSize = newSize
      this.getNewsList()
    },
    // 监听页码值改变事件
    handleCurrentChange(newPage){
      // console.log(newPage)
      this.queryInfo.pageNum = newPage
      this.getNewsList()
    },
    // 获取新闻列表
    async getNewsList(){
      const {data:res } = await this.$http.get(`/dqxmall/news/list?page=${this.queryInfo.pageNum}&limit=${this.queryInfo.pageSize}&key=${this.queryInfo.query}`)
      // console.log(res)
      this.total = res.page.totalCount
      this.newsInfo = res.page.list
    },
    // 
    async updateNews(){
      const {data:res} = await this.$http.post('/dqxmall/news/update',this.updateNewsInfo)
      if(res.code!== 0) return this.$message.error("修改失败")
      this.$message.success("修改成功")
      this.getNewsList()
      this.dialogEditVisible = false
    },
    // 修改对话框显示
    showEditDialog(row){
      this.updateNewsInfo = row
      this.dialogEditVisible = true
    },
    // 关闭修改对话框
    editDialogClose(){
      this.$refs.editFormRef.resetFields()
    },
    // 删除新闻
    async removeNewsInfo(newsInfoId){
      const {data:res} = await this.$http.post('/dqxmall/news/delete',[newsInfoId])
      if(res.code!== 0) return this.$message.error("删除失败")
      this.$message.success("删除成功")
      this.getNewsList()
    }
  }

}

</script>
<style lang='less' scoped>
.headimag{
  height: 90px;
  width: 90px;
}
.updateNewsContent{
  width: 530px;
}
.newsContent,.newsTitle{
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}
</style>